<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Article; 
use App\Comment;
use App\Category;
use App\Paper;

class PaperController extends Controller
{
    //
    public function index(Request $request){
        $papers = Paper::all();
        return view('admin/paper/index',['papers'=> $papers]);
    }

    public function create(){
        $categories = Category::all();
        return view('admin/paper/create')->with('categories', $categories);
    }

    public function store(Request $request){
        $this->validate($request, [
            'title'=>'required|unique:articles|max:255',
            'body'=>'required',
        ]);
        $category = $request->get('category');
        $categories = Category::where('name', $category)->first();
        $page_num = Article::where('category', $category)->count();
        $categories->page_num = $page_num + 1;
        $categories->save();

        $article = new Article();
        $article->title = $request->get('title');
        $article->body = $request->get('body');
        $article->category = $category;
        // var_dump($request->file('imgOne'));
        if ($request->hasFile('imgOne')) {
            $file = $request->file('imgOne');
            if($file->isValid()){ 
                $clientName = $file -> getClientOriginalName();    //客户端文件名称..
                // $tmpName = $file ->getFileName();   //缓存在tmp文件夹中的文件名例如php8933.tmp 这种类型的.
                // $realPath = $file -> getRealPath();     //这个表示的是缓存在tmp文件夹下的文件的绝对路径
                $entension = $file -> getClientOriginalExtension();   //上传文件的后缀.
                // $mimeTye = $file -> getMimeType();    //也就是该资源的媒体类型
                $newName = $newName = md5(date('ymdhis').$clientName).".".$entension;    //定义上传文件的新名称
                // $path = $file -> move('storage/uploads',$newName);    //把缓存文件移动到制定文件夹

                $newName = md5(date("Y-m-d-H-i-s").'-'.$clientName).'.'.$entension;
                $file->move(public_path().'/img/cover', $newName);
            }
        }
        $article->cover_img = $newName;
        $article->user_id = $request->user()->id;

        if($article->save()){
            return redirect('admin/paper');
        }else{
            return redirect('admin/paper')->back()->withInput()->withErrors('新增失败！');
        }
    }

    public function edit($id) { 
        $article = Article::find($id); 
        return view('admin/paper/edit')->with('article', $article); 
    }

    public function show($id){
        $article = Article::find($id);
        $article->read_total += 1;
        $article->save();
        return view('paper/show')->withArticle(Article::with('hasManyComments')->with('hasManyUsers')->find($id));
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'required|unique:articles,title,'.$id.'|max:255',
            'body' => 'required', 
        ]);

        $article = Article::find($id);
        $article->title = $request->get('title');
        $article->body = $request->get('body');

        if ($article->save()) {
            return redirect('admin/paper');
        } else {
            return redirect('admin/paper')->back()->withInput()->withErrors('更新失败！');
        }
    }

    public function destroy($id){
        $article = Article::find($id); 
        $category = Category::where('name', $article->category)->first();
        $category->page_num -= 1;
        $category->save();
        if($article->delete()){
            return redirect('admin/paper')->withErrors('删除成功！');
        }else{
            return redirect('admin/paper')->back()->withInput()->withErrors('删除失败！');
        }
    }

    public function upvote($id){
        $article = Article::find($id);
        $article->upvote_total += 1;
        $upvote_total = $article->upvote_total;
        $article->save();
        return $upvote_total;
    }

    public function review(Request $request){
        $paper_id = $request->get('paper');
        var_dump($paper_id);
        $paper = Paper::where('id', $paper_id)->first();
        $paper->reviewer = $request->user()->id;
        $paper->result = 1;
        if ($paper->save()){
            return redirect('admin/paper')->withErrors('审核成功！');
        }else{
            return redirect('admin/paper')->withErrors('审核失败！');
        }

    }

    public function resetReview(Request $request){
        $paper_id = $request->get('paper');
        $paper = Paper::where('id', $paper_id)->first();
        $paper->reviewer = $request->user()->id;
        $paper->result = 0;
        if ($paper->save()){
            return redirect('admin/paper')->withErrors('取消成功！');
        }else{
            return redirect('admin/paper')->withErrors('取消失败！');
        }

    }
}
